package com.xxm

import android.Manifest
import android.content.pm.PackageManager
import android.os.Build
import android.widget.Toast
import androidx.databinding.DataBindingUtil
import com.xxm.camera.camera2.Camera2Activity
import com.xxm.camera.camerax.CameraXActivity
import com.xxm.databinding.ActivityNavigationBinding
import com.xxm.mediacodec.MediaCodecActivity
import com.xxm.opengl.triangle.TriangleActivity
import com.zhangws.comman.base.BaseActivity
import com.zhangws.comman.base.startActivityK

class NavigationActivity: BaseActivity<ActivityNavigationBinding>() {
    private val PERMISSION_REQUEST_CODE: Int = 1

    private val permissions = listOf(
        Manifest.permission.CAMERA,
        Manifest.permission.WRITE_EXTERNAL_STORAGE
    )

    override fun getViewDataBinding(): ActivityNavigationBinding =DataBindingUtil.setContentView(this,R.layout.activity_navigation)

    override fun initView() {
        viewBinding.deviceInfo.text="ABI:${Build.SUPPORTED_ABIS.toList()}\n"
    }

    override fun initListener() {
        viewBinding.btnGLTriangle.setOnClickListener {
            startActivityK<TriangleActivity>()
        }

        viewBinding.btnPlaLocal.setOnClickListener {
            startActivityK<MediaCodecActivity>()
        }

        viewBinding.btnCamera.setOnClickListener {
            startActivityK<CameraXActivity>()
        }
        viewBinding.btnCamera2.setOnClickListener {
            startActivityK<Camera2Activity>()
        }
    }

    override fun initData() {
        hasPermission()
    }

    private fun hasPermission() {
        var needRequest = false
        permissions.forEach {
            if (checkSelfPermission(it) != PackageManager.PERMISSION_GRANTED) {
                needRequest = true
            }
        }
        if (needRequest) {
            requestPermissions(permissions.toTypedArray(), PERMISSION_REQUEST_CODE)
        }
    }

    override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults)
        if (requestCode == PERMISSION_REQUEST_CODE) {
            grantResults.forEach {
                if (it == PackageManager.PERMISSION_DENIED) {
                    Toast.makeText(this, "需要授权", Toast.LENGTH_SHORT).show()
                }
            }
        }
    }
}